import Vue from "vue"
import VueRouter from "vue-router"
import Home from "@/pages/Home"
import Search from "@/pages/Search"
import Login from "@/pages/Login"
import Register from "@/pages/Register"
import Test from '@/pages/Test'
Vue.use(VueRouter)

// 保存下来VueRouter.prototypy当中的那个push
let originPush = VueRouter.prototype.push
// 1.调用push或者replace 可以传递成功和失败得回调函数
// 把VueRouter.prototype的push重新更改 组件中使用的是自己写的这一个push
VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve === undefined && reject === undefined) {
    return originPush.call(this, location).catch(() => {})
  } else {
    // 传了一个或者两个
    return originPush.call(this, location, resolve, reject)
  }
}

let originReplace = VueRouter.prototype.replace
VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve === undefined && reject === undefined) {
    return originReplace.call(this, location).catch(() => {})
  } else {
    // 传了一个或者两个
    return originReplace.call(this, location, resolve, reject)
  }
}

export default new VueRouter({
  routes: [
    {
      path: "/home",
      component: Home,
    },
    {
      path: "/search/:keyword?",
      component: Search,
      name: "search",
      props: (route) => ({ ...route.params, ...route.query }), // 小括号包起来就会返回一个对象，所以要在外面写一个小括号
    }, // 如果只是一个花括号 就会认为里面是箭头函数的返回值 返回一个函数体   如果用小括号就会认为是返回一个函数
    {
      path: "/login",
      component: Login,
      meta: {
        noHidden: true,
      },
    },
    {
      path: "/register",
      component: Register,
      meta: {
        // 元数据
        noHidden: true, //这个noHidden 只是随便起的名字 关键的是后面这个true
      },
    },
    {
      path:'/test',
      component:Test
    },
    {
      path: "/",
      redirect: "/home",
    },
  ],
})
